<template>
   <div class="app-container">
      <el-card shadow="hover" v-show="showSearch">
         <el-form :model="queryParams" ref="queryRef" :inline="true" >
            <el-form-item label="数据源名称" prop="name">
               <el-input v-model="queryParams.name" placeholder="请输入数据源名称" clearable style="width: 200px"
                  @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="主机名" prop="host">
               <el-input v-model="queryParams.host" placeholder="请输入主机名" clearable style="width: 200px"
                  @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="数据库名称" prop="dbName">
               <el-input v-model="queryParams.dbName" placeholder="请输入数据库名称" clearable style="width: 200px"
                  @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item>
               <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
               <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
         </el-form>
      </el-card>

      <el-card shadow="hover" style="margin-top: 10px;">
         <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
               <el-button type="primary" plain icon="Plus" @click="handleAdd">新增</el-button>
            </el-col>
            <el-col :span="1.5">
               <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate">修改</el-button>
            </el-col>
            <el-col :span="1.5">
               <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete" >删除</el-button>
            </el-col>
            <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
         </el-row>
      
      
         <el-table v-loading="loading" border :data="dataSourceList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="50" align="center" />
            <el-table-column label="序号" width="55" align="center" prop="id" />
            <el-table-column label="数据源名称" align="center" width="100" prop="name" :show-overflow-tooltip="true" />
            <el-table-column label="类型" align="center" width="100" prop="type" :show-overflow-tooltip="true" />
            <el-table-column label="主机名" align="center" prop="host" width="100"></el-table-column>
            <el-table-column label="端口" align="center" prop="port" width="100"></el-table-column>
            <el-table-column label="数据库名称" align="center" prop="dbName" width="100" />
            <el-table-column label="用户名" align="center" prop="userName" width="150"></el-table-column>
            <el-table-column label="密码" align="center" prop="password" width="150"></el-table-column>
            <el-table-column label="初始化连接数" align="center" prop="initialSize" width="100"></el-table-column>
            <el-table-column label="最大连接数" align="center" prop="maxPoolSize" width="100"></el-table-column>
            <el-table-column label="最小连接数" align="center" prop="minPoolSize" width="100"></el-table-column>
            <el-table-column label="最大空闲连接数" align="center" prop="maxIdle" width="120"></el-table-column>
            <el-table-column label="空闲连接超时时间" align="center" prop="idleTimeout" width="130"></el-table-column>
            <el-table-column label="描述" align="center" prop="remark" width="150"></el-table-column>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
               <template #default="scope">
                  <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)">修改</el-button>
                  <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)">删除</el-button>
               </template>
            </el-table-column>
         </el-table>
         <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"  v-model:limit="queryParams.pageSize" @pagination="getList" />
      </el-card>

      <el-dialog :title="title" v-model="open" width="900px" append-to-body>
         <el-form ref="formRef" :model="form" :rules="rules" label-width="120px">
            <el-row>
               <el-col :span="12">
                  <el-form-item label="数据源名称" prop="name">
                     <el-input v-model="form.name" placeholder="请输入数据源名称" />
                  </el-form-item>
               </el-col>
               <el-col :span="12">
                  <el-form-item label="类型" prop="type">
                     <el-select v-model="form.type" placeholder="请选择类型" style="width: 100%">
                        <el-option v-for="dict in sysDataSourceType" :key="dict.value" :label="dict.label"  :value="dict.value" />
                     </el-select>
                  </el-form-item>
               </el-col>
               <el-col :span="12">
                  <el-form-item label="主机名" prop="host">
                     <el-input v-model="form.host" placeholder="请输入主机名" />
                  </el-form-item>
               </el-col>
               <el-col :span="12">
                  <el-form-item label="端口" prop="port">
                     <el-input-number v-model="form.port" :min="1" :max="65535" style="width: 100%" />
                  </el-form-item>
               </el-col>
               <el-col :span="12">
                  <el-form-item label="数据库名称" prop="dbName">
                     <el-input v-model="form.dbName" placeholder="请输入数据库名称" />
                  </el-form-item>
               </el-col>
               <el-col :span="12">
                  <el-form-item label="用户名" prop="userName">
                     <el-input v-model="form.userName" placeholder="请输入用户名" />
                  </el-form-item>
               </el-col>
               <el-col :span="12">
                  <el-form-item label="密码" prop="password">
                     <el-input v-model="form.password" placeholder="请输入密码" show-password />
                  </el-form-item>
               </el-col>
               <el-col :span="12">
                  <el-form-item label="初始化连接数" prop="initialSize">
                     <el-input-number v-model="form.initialSize" :min="1" style="width: 100%" />
                  </el-form-item>
               </el-col>
               <el-col :span="12">
                  <el-form-item label="最大连接数" prop="maxPoolSize">
                     <el-input-number v-model="form.maxPoolSize" :min="1" style="width: 100%" />
                  </el-form-item>
               </el-col>
               <el-col :span="12">
                  <el-form-item label="最小连接数" prop="minPoolSize">
                     <el-input-number v-model="form.minPoolSize" :min="1" style="width: 100%" />
                  </el-form-item>
               </el-col>
               <el-col :span="12">
                  <el-form-item label="最大空闲连接数" prop="maxIdle">
                     <el-input-number v-model="form.maxIdle" :min="0" style="width: 100%" />
                  </el-form-item>
               </el-col>
               <el-col :span="12">
                  <el-form-item label="空闲连接超时时间" prop="idleTimeout">
                     <el-input-number v-model="form.idleTimeout" :min="0" style="width: 100%" />
                  </el-form-item>
               </el-col>
               <el-col :span="24">
                  <el-form-item label="描述" prop="remark">
                     <editor v-model="form.remark" :min-height="192" />
                  </el-form-item>
               </el-col>
            </el-row>
         </el-form>
         <template #footer>
            <div class="dialog-footer">
               <el-button @click="cancel">取 消</el-button>
               <el-button type="primary" @click="submitForm">确 定</el-button>
            </div>
         </template>
      </el-dialog>

   </div>
</template>

<script setup name="dataSource">

const { proxy } = getCurrentInstance()

const dataSourceList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")

const data = reactive({
   form: {
      id: undefined,
      name: '',
      type: '',
      host: '',
      port: 3306,
      dbName: '',
      userName: '',
      password: '',
      initialSize: 5,
      maxPoolSize: 20,
      minPoolSize: 5,
      maxIdle: 10,
      idleTimeout: 60000,
      remark: ''
   },
   rules: {
      name: [{ required: true, message: '数据源名称不能为空', trigger: 'blur' }],
      type: [{ required: true, message: '请选择数据源类型', trigger: 'change' }],
      host: [{ required: true, message: '主机名不能为空', trigger: 'blur' }],
      port: [{ required: true, message: '端口不能为空', trigger: 'blur' }],
      dbName: [{ required: true, message: '数据库名称不能为空', trigger: 'blur' }]
   },
   queryParams: {
      pageNum: 1,
      pageSize: 10,
      name: undefined,    // 数据源名称
      host: undefined,    // 主机名称
      dbName: undefined   // 数据库名称
   }
})
const { queryParams, form, rules } = toRefs(data)


/** 查询数据源列表 */
function getList() {
   loading.value = true
   dataSourceList.value = [
  {
    "id": 1,
    "name": "MySQL-Production",
    "type": "mysql",
    "host": "192.168.1.10",
    "port": 3306,
    "dbName": "prod_db",
    "userName": "admin",
    "password": "secure_password_123",
    "initialSize": 5,
    "maxPoolSize": 20,
    "minPoolSize": 5,
    "maxIdle": 10,
    "idleTimeout": 60000,
    "remark": "生产环境数据库"
  },
  {
    "id": 2,
    "name": "PostgreSQL-Test",
    "type": "postgresql",
    "host": "192.168.1.11",
    "port": 5432,
    "dbName": "test_db",
    "userName": "test_user",
    "password": "test_pass_456",
    "initialSize": 3,
    "maxPoolSize": 15,
    "minPoolSize": 3,
    "maxIdle": 8,
    "idleTimeout": 30000,
    "remark": "测试环境PostgreSQL数据库"
  },
  {
    "id": 3,
    "name": "Oracle-Dev",
    "type": "oracle",
    "host": "192.168.1.12",
    "port": 1521,
    "dbName": "dev_orcl",
    "userName": "developer",
    "password": "dev_password",
    "initialSize": 2,
    "maxPoolSize": 10,
    "minPoolSize": 2,
    "maxIdle": 5,
    "idleTimeout": 45000,
    "remark": "开发用Oracle数据库"
  }
   ]
   loading.value = false
}

/** 取消按钮 */
function cancel() {
   open.value = false
   reset()
}

/** 表单重置 */
function reset() {
   form.value = {
    id: undefined,
    name: '',
    type: '',
    host: '',
    port: 3306,
    dbName: '',
    userName: '',
    password: '',
    initialSize: 5,
    maxPoolSize: 20,
    minPoolSize: 5,
    maxIdle: 10,
    idleTimeout: 60000,
    remark: ''
  }
  proxy.resetForm("formRef")
}

/** 搜索按钮操作 */
function handleQuery() {
   queryParams.value.pageNum = 1
   getList()
}

/** 重置按钮操作 */
function resetQuery() {
   proxy.resetForm("queryRef")
   handleQuery()
}

/** 多选框选中数据 */
function handleSelectionChange(selection) {
   ids.value = selection.map(item => item.id)
   single.value = selection.length != 1
   multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
   reset()
   open.value = true
   title.value = "添加数据源"
}

/**修改按钮操作 */
function handleUpdate(row) {
   reset()
   const Id = row.id || ids.value
   getDataSource(Id).then(response => {
      form.value = response.data
      open.value = true
      title.value = "修改数据源"
   })
}

/** 提交按钮 */
function submitForm() {
   proxy.$refs["formRef"].validate(valid => {
      if (valid) {
         if (form.value.id != undefined) {
            updateDataSource(form.value).then(response => {
               proxy.$modal.msgSuccess("修改成功")
               open.value = false
               getList()
            })
         } else {
            addDataSource(form.value).then(response => {
               proxy.$modal.msgSuccess("新增成功")
               open.value = false
               getList()
            })
         }
      }
   })
}

/** 删除按钮操作 */
function handleDelete(row) {
   const Ids = row.id || ids.value
   proxy.$modal.confirm('是否确认删除"' + row.name + '"的数据项？').then(function () {
      return delDataSource(Ids)
   }).then(() => {
      getList()
      proxy.$modal.msgSuccess("删除成功")
   }).catch(() => { })
}

getList()
</script>
